package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Query.SortDirection;

import data.Guest;
import data.GuestMapping;



public class GuestMappingLogic extends AbstractBL {
	public GuestMappingLogic(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	public List<GuestMapping> GenerateGuestMapping(long hotelId)
	{
		Guest guest = new GuestLogic(req,resp).GetAllGuest().get(0);
		List<GuestMapping> list = new ArrayList<GuestMapping>();
		GuestMapping gm = new GuestMapping();
		gm.setGuestString(String.valueOf(guest.getId()));
		gm.setHotelId(hotelId);
		gm.setDisplayId(GetMaxGuestDisplayId(hotelId));
		gm.setAvaliable(true);
		gm.setStatus(true);
		list.add(gm);
		
		return list;
	}
	
	public GuestMapping GetGuestMapping(long id)
	{
		Key key = KeyFactory.createKey(EntityName.GuestMapping, id);
		try {
			Entity lang = datastore.get(key);
			GuestMapping c = new GuestMapping();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
		
	
	public GuestMapping GetGuestMappingByHotelId(long hotelId)
	{
		Query q = new Query(EntityName.GuestMapping);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		q.setFilter(filterHotel);
		
		return GetGuestMapping(datastore.prepare(q).asSingleEntity());
	}
	
	public GuestMapping GetGuestMappingByGuestId(long hotelId,String guest)
	{
		Query q = new Query(EntityName.GuestMapping);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterGuest = new FilterPredicate("guestId",FilterOperator.EQUAL,guest);
		Filter filter = CompositeFilterOperator.and(filterHotel, filterGuest);
		q.setFilter(filter);
		
		return GetGuestMapping(datastore.prepare(q).asSingleEntity());
	}
	
	public List<GuestMapping> GetAllGuestMapping(long hotelId)
	{
		Query q = new Query(EntityName.GuestMapping);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		q.setFilter(filterHotel);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public GuestMapping GetGuestMapping(Entity entity)
	{
		GuestMapping item = new GuestMapping();
		item.setEntity(entity);
		return item;
	}
	
	public List<GuestMapping> GetList(List<Entity> entities)
	{
		List<GuestMapping> list = new ArrayList<GuestMapping>();
		for(Entity e : entities)
		{
			GuestMapping a = new GuestMapping();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}
	
	public long GetMaxGuestDisplayId(long hotelId)
	{
		Query q = new Query(EntityName.GuestMapping);

		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		q.setFilter(filterHotel).addSort("displayId", SortDirection.DESCENDING);

		List<GuestMapping> list = GetList(datastore.prepare(q).asList(
				FetchOptions.Builder.withLimit(1)));
		if(list.size()>0)
			return list.get(0).getDisplayId();
		return 0;
	}
}
